package com.hina.rec.application.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hina.rec.application.convert.SettlePaymentConvert;
import com.hina.rec.application.service.SettlePaymentService;
import com.hina.rec.domain.mapper.SettlePaymentMapper;
import com.hina.rec.domain.model.SettlePaymentDO;
import com.hina.rec.endpoint.req.SettlePaymentSaveReq;
import com.hina.rec.endpoint.req.SettlePaymentSearchReq;
import com.hina.rec.endpoint.req.SettlePaymentUpdateReq;
import com.hina.rec.endpoint.res.SettlePaymentRes;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 结算单回款服务实现类
 *
 * @author dequal
 * @since 2025-05-23
 */
@Service
@RequiredArgsConstructor
public class SettlePaymentServiceImpl implements SettlePaymentService {

    private final SettlePaymentMapper settlePaymentMapper;

    @Override
    public Page<SettlePaymentRes> page(SettlePaymentSearchReq req) {
        // 构建查询条件
        LambdaQueryWrapper<SettlePaymentDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(req.getSettleName()), SettlePaymentDO::getSettleName, req.getSettleName())
                .orderByDesc(SettlePaymentDO::getCreatedAt);

        // 执行分页查询
        Page<SettlePaymentDO> page = settlePaymentMapper.selectPage(new Page<>(req.getPageNum(), req.getPageSize()), wrapper);
        
        // 转换结果
        Page<SettlePaymentRes> result = new Page<>();
        result.setRecords(page.getRecords().stream().map(SettlePaymentConvert.INSTANCE::toRes).toList());
        result.setTotal(page.getTotal());
        result.setCurrent(page.getCurrent());
        result.setSize(page.getSize());
        
        return result;
    }

    @Override
    public void save(SettlePaymentSaveReq req) {
        // 转换为DO对象
        SettlePaymentDO settlePaymentDO = SettlePaymentConvert.INSTANCE.toDO(req);
        
        // 保存数据
        settlePaymentMapper.insert(settlePaymentDO);
    }

    @Override
    public void update(SettlePaymentUpdateReq req) {
        // 转换为DO对象
        SettlePaymentDO settlePaymentDO = SettlePaymentConvert.INSTANCE.toDO(req);
        
        // 更新数据
        settlePaymentMapper.updateById(settlePaymentDO);
    }

    @Override
    public void delete(Integer id) {
        // 删除数据
        settlePaymentMapper.deleteById(id);
    }
} 